<!--
 * @Author: your name
 * @Date: 2022-04-25 09:26:36
 * @LastEditTime: 2022-04-25 10:20:47
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \算法测试\day0906\链表环.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function ListNode(num, next) {
            this.num = num === undefined ? 0 : num;
            this.next = next === undefined ? null : next;
        }
        let a = new ListNode(1);
        let b = new ListNode(2);
        let c = new ListNode(3);
        a.next = b;
        b.next = c;
        c.next = b;
        var detectCycle = function (head) {
            //let num = head;
            let set = new Set();
            while (head) {
                if (set.has(head)) {
                    return head;
                }
                set.add(head);
                head = head.next;
            }
            return head;
        };
        console.log(detectCycle(a));
    </script>
</body>

</html>